using System;
using System.Web.UI.HtmlControls;

namespace Library.WebControls.SimpleTreeView {
	internal class TreeViewItemNode : HtmlGenericControl {
		internal const string ItemClass = "tvitem";
		internal TreeViewItemNode(object key, int level)
			: base("div") {
			Attributes["class"] = ItemClass;
			Attributes.Add("sptnk", String.Format("{0}", key));
			Attributes.Add("sptnl", level.ToString());
		}

		private TreeViewItemNodeTitle _treeViewTitle;
		internal TreeViewItemNodeTitle TreeViewTitle {
			get {
				if (_treeViewTitle == null) {
					_treeViewTitle = new TreeViewItemNodeTitle();
					//var padddingLeft = new HtmlGenericControl("div");
					//padddingLeft.Attributes["class"] = "tvItemPadLeft";
					//padddingLeft.Controls.Add(_treeViewTitle);
					Controls.Add(_treeViewTitle);
				}
				return _treeViewTitle;
			}
		}


		internal class TreeViewItemNodeTitle : HtmlGenericControl {
			internal TreeViewItemNodeTitle()
				: base("div") {
				Attributes["class"] = "tvtitle";
			}

			internal void CreateCollapse(string clientName) {
				CreateSpan("tvcollapse", String.Format("{0}.Expend(this)", clientName));

			}
			internal void CreateExpend(string clientName) {
				CreateSpan("tvexpend", String.Format("{0}.Collapse(this)", clientName));
			}
			internal void CreateNoImage(string clientName) {
				CreateSpan("tvnoimage", String.Empty);
			}

			private HtmlGenericControl CreateSpan(string clazz, string onlick) {
				var sp = new HtmlGenericControl("div");
				sp.Attributes["class"] = clazz;
				sp.Attributes["onclick"] = onlick;
				Controls.Add(sp);
				return sp;
			}

			private SpTreeNodeItemTemplate _spanContent;
			internal SpTreeNodeItemTemplate SpanContent {
				get {
					if (_spanContent == null) {
						_spanContent = new SpTreeNodeItemTemplate();
					
						Controls.Add(_spanContent);
					}

					return _spanContent;
				}
			}
		}
	}

}
